package leetbook.stack;

import java.util.ArrayList;
import java.util.List;

public class Solution341 {

    ArrayList<Integer> allNum = null; //遍历完num存在这里
    int curIdx; //当前下标

    public Solution341(List<NestedInteger> nestedList) {
        this.curIdx = -1;
        this.allNum = new ArrayList<>();
        for(NestedInteger nestedInt:nestedList){
            dfs(this.allNum, nestedInt);
        }
    }

    public Integer next() {
        return allNum.get(++curIdx);
    }

    public boolean hasNext() {
        return curIdx+1<=allNum.size()-1;
    }

    public void dfs(ArrayList<Integer> allNum, NestedInteger nestedInteger) {
        if(nestedInteger.isInteger()) allNum.add(nestedInteger.getInteger());
        for(NestedInteger nestedInt:nestedInteger.getList()){
            dfs(allNum, nestedInt);
        }
    }

}


class NestedInteger {
    // @return true if this NestedInteger holds a single integer, rather than a nested list.
    public boolean isInteger(){
        return true;
    }
    // @return the single integer that this NestedInteger holds, if it holds a single integer
    // Return null if this NestedInteger holds a nested list
    public Integer getInteger(){
        return 0;
    }
    // @return the nested list that this NestedInteger holds, if it holds a nested list
    // Return empty list if this NestedInteger holds a single integer
    public List<NestedInteger> getList(){
        return new ArrayList<>();
    }
}
